package com.chuangke.mds.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.chuangke.admin.entity.SysUser;
import com.chuangke.context.service.ContextService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.common.idgen.IdManager;
import com.chuangke.common.trans.annotation.TransResult;
import com.chuangke.mds.dao.ProblemSolutionDao;
import com.chuangke.mds.entity.ProblemSolution;
import com.chuangke.mds.entity.TechnologyProcess;
import com.chuangke.mds.service.ProblemSolutionService;
import com.chuangke.mds.service.TechnologyProcessService;

import cn.hutool.core.util.StrUtil;

@Service
@Transactional(rollbackFor = Throwable.class)
public class ProblemSolutionImpl implements ProblemSolutionService {
	@Autowired
	private ContextService contextService;
	@Autowired
	private ProblemSolutionDao psDao;
	@Autowired
	private TechnologyProcessService technologyProcessService;
	
	@Override
	public String save(ProblemSolution model) {
		return saveOrUpdate(model);
	}
	
	@Override
	public void update(ProblemSolution model) {
		saveOrUpdate(model);
	}

	@Override
	public String saveOrUpdate(ProblemSolution model) {
		if (StrUtil.isBlank(model.getId())) {
			doSave(model) ;
		}else{
			doUpdate(model) ;
		}
		return model.getId() ;
	}
	
	private void doSave(ProblemSolution model) {
		model.setId(IdManager.nextId());
		TechnologyProcess process = model.getProcess();
		if (process != null) {
			String processId = technologyProcessService.saveOrUpdate(process);
			model.setProcessId(processId);
		}
		psDao.save(model) ;
	}

	private void doUpdate(ProblemSolution model) {
		psDao.updateById(model) ;
		TechnologyProcess process = model.getProcess();
		if (process != null) {
			technologyProcessService.saveOrUpdate(process);
		}else{
			List<String> processIds = new ArrayList<>();
			processIds.add(model.getProcessId());
			technologyProcessService.delete(processIds);
		}
	}

	@Override
	public void delete(List<String> billIds) {
		List<ProblemSolution> pss = psDao.listByIds(billIds);
		List<String> processIds = pss.stream().map(ProblemSolution::getProcessId).collect(Collectors.toList());
		technologyProcessService.delete(processIds);

		psDao.remove(new QueryWrapper<ProblemSolution>().lambda().in(ProblemSolution::getId, billIds));
	}

	@Override
	@TransResult
	public ProblemSolution load(String billId) {
		ProblemSolution ps = psDao.getById(billId);
		if (ps != null&&!StringUtils.isEmpty(ps.getProcessId())) {
			TechnologyProcess process = technologyProcessService.load(ps.getProcessId());
			ps.setProcess(process);
		}
		return ps;
	}

	@Override
	@TransResult
	public IPage<ProblemSolution> page(CkPageQuery page) {
		QueryWrapper<ProblemSolution> qw = new QueryWrapper<>();
		if(!StringUtils.isEmpty(page.getAsString("name"))) {
			if ("name".equals(page.getAsString("filterSelection"))){
				qw.lambda().like(ProblemSolution::getName, page.getAsString("name"));
			}else if ("description".equals(page.getAsString("filterSelection"))){
				qw.lambda().like(ProblemSolution::getDescription, page.getAsString("name"));
			}else if ("solutionIdea".equals(page.getAsString("filterSelection"))){
				qw.lambda().like(ProblemSolution::getSolutionIdea, page.getAsString("name"));
			} else if ("concreteStep".equals(page.getAsString("filterSelection"))) {
				qw.lambda().like(ProblemSolution::getConcreteStep, page.getAsString("name"));
			}else if ("desiredResult".equals(page.getAsString("filterSelection"))){
				qw.lambda().like(ProblemSolution::getDesiredResult, page.getAsString("name"));
			}
		}
		SysUser user= contextService.getCurrentUser();
		String userId = user.getId();
		if (!"admin".equals(userId)) {
			qw.lambda().eq(ProblemSolution::getCreatedBy, userId);
		}
		qw.lambda().orderByDesc(ProblemSolution::getCreatedAt) ;
		return psDao.page(page.getPage(),qw);
	}

}
